package com.force.jianghuling.base;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.force.jianghuling.R;
import com.force.jianghuling.utils.TitleBuilder;
import com.force.jianghuling.utils.ToastUtils;


public abstract class BaseFragment extends Fragment {

    protected String TAG;
    protected BaseActivity activity;
    protected boolean isVisible;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        TAG = getClass().getSimpleName();
        activity = (BaseActivity) getActivity();
    }

    /**
     * 顶部标题(带返回键)
     *
     * @param title
     * @return
     */
    protected TitleBuilder initBackTitle(String title, View view) {
        return new TitleBuilder(view)
                .setTitleText(title)
                .setLeftImage(R.mipmap.ic_back)
                .setLeftOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        activity.finish();
                    }
                });
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);

        if (getUserVisibleHint()) {
            isVisible = true;
            onVisible();
        } else {
            isVisible = false;
            onInvisible();
        }

    }

    protected void onInvisible() {
        lazyLoad();
    }

    protected void onVisible() {
        gone();
    }

    //不可见

    protected abstract void gone();

    /**
     * 延迟加载  子类必须重写此方法
     */
    //可见
    protected abstract void lazyLoad();

    /**
     * 不带返回键的标题栏
     *
     * @param title 标题名称
     * @param view  初始化加载的view
     * @return
     */
    protected TitleBuilder initTitle(String title, View view) {
        return new TitleBuilder(view)
                .setTitleText(title);
    }

    protected void intent2Activity(Class<? extends Activity> tarActivity) {
        Intent intent = new Intent(activity, tarActivity);
        startActivity(intent);
    }

    protected void showToast(String msg) {
        ToastUtils.showToast(activity, msg, Toast.LENGTH_SHORT);
    }

    protected void showLog(String msg) {
        Log.i(TAG, msg);
    }

    protected void showProgressDialog() {
        activity.showProgressDialog();
    }

    protected void dismissProgressDialog() {
        activity.dismissProgressDialog();
    }

}
